package com.ss.android.common.applog;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.bytedance.common.utility.Logger;

import org.json.JSONArray;
import org.json.JSONObject;

final class l {
    final class a extends SQLiteOpenHelper {
        public a(Context arg4) {
            super(arg4, l.a, null, 7);
        }

        public final void onCreate(SQLiteDatabase arg5) {
            try {
                arg5.execSQL("CREATE TABLE session ( _id INTEGER PRIMARY KEY AUTOINCREMENT, value VARCHAR NOT NULL, timestamp INTEGER, duration INTEGER, non_page INTEGER, app_version VARCHAR, version_code INTEGER, pausetime INTEGER,launch_sent INTEGER NOT NULL DEFAULT 0 )");
                arg5.execSQL("CREATE TABLE event ( _id INTEGER PRIMARY KEY AUTOINCREMENT, category VARCHAR, tag VARCHAR, label VARCHAR, value INTEGER, ext_value INTEGER, ext_json TEXT, user_id INTEGER, timestamp INTEGER, session_id INTEGER )");
                arg5.execSQL("CREATE TABLE page ( _id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, duration INTEGER, session_id INTEGER )");
                arg5.execSQL("CREATE TABLE queue ( _id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT, is_crash INTEGER NOT NULL DEFAULT 0, log_type INTEGER NOT NULL DEFAULT 0, timestamp INTEGER, retry_count INTEGER, retry_time INTEGER )");
                arg5.execSQL("CREATE TABLE mon_log ( _id INTEGER PRIMARY KEY AUTOINCREMENT, log_type VARCHAR, value TEXT )");
                arg5.execSQL("CREATE TABLE misc_log ( _id INTEGER PRIMARY KEY AUTOINCREMENT, log_type VARCHAR, value TEXT, session_id INTEGER  )");
            } catch (Exception v0) {
                Logger.e("AppLog", "create db exception " + v0);
            }
        }

        public final void onUpgrade(SQLiteDatabase arg2, int arg3, int arg4) {
            if (arg3 < 2) {
                arg2.execSQL("ALTER TABLE event ADD COLUMN user_id INTEGER");
            }

            if (arg3 < 3) {
                arg2.execSQL("ALTER TABLE session ADD COLUMN launch_sent INTEGER NOT NULL DEFAULT 0");
            }

            if (arg3 < 4) {
                arg2.execSQL("ALTER TABLE queue ADD COLUMN is_crash INTEGER NOT NULL DEFAULT 0");
            }

            if (arg3 < 5) {
                arg2.execSQL("ALTER TABLE event ADD COLUMN ext_json TEXT");
            }

            if (arg3 < 6) {
                arg2.execSQL("ALTER TABLE queue ADD COLUMN log_type INTEGER NOT NULL DEFAULT 0");
                arg2.execSQL("CREATE TABLE mon_log ( _id INTEGER PRIMARY KEY AUTOINCREMENT, log_type VARCHAR, value TEXT )");
            }

            if (arg3 < 7) {
                arg2.execSQL("CREATE TABLE misc_log ( _id INTEGER PRIMARY KEY AUTOINCREMENT, log_type VARCHAR, value TEXT, session_id INTEGER  )");
            }
        }
    }

    static volatile String a;
    private static String[] b;
    private static String[] c;
    private static String[] d;
    private static String[] e;
    private static String[] f;
    private static String[] g;
    private static final Object h = new Object();
    private static l i;
    private SQLiteDatabase j;

    static {
        l.a = "ss_app_log.db";
        l.b = new String[]{"_id", "name", "duration", "session_id"};
        l.c = new String[]{"_id", "value", "is_crash", "timestamp", "retry_count", "retry_time", "log_type"};
        l.d = new String[]{"_id", "value", "timestamp", "duration", "non_page", "app_version", "version_code", "pausetime", "launch_sent"};
        l.e = new String[]{"_id", "category", "tag", "label", "value", "ext_value", "ext_json", "user_id", "timestamp", "session_id"};
        l.f = new String[]{"_id", "log_type", "value", "session_id"};
        l.g = new String[]{"_id", "log_type", "value"};
    }

    private l(Context arg2) {

        this.j = new a(arg2).getWritableDatabase();
    }

    public static void a() {
        Object v1 = l.h;
        synchronized (v1) {
            if (l.i != null) {
                l.i.c();
            }
        }
    }

    public static l a(Context arg3) {
        Object v1 = l.h;
        synchronized (v1) {
            if (l.i == null) {
                l.i = new l(arg3.getApplicationContext());
            }
        }
        return l.i;
    }

    public final long a(p arg5) {
        long v0_2;
        synchronized (this) {
            if (this.j == null || !this.j.isOpen()) {
                Logger.w("AppLog", "db not establish and open");
                v0_2 = -1;
            } else {
                ContentValues v0_1 = new ContentValues();
                v0_1.put("category", arg5.a);
                v0_1.put("tag", arg5.b);
                if (!android.support.design.a.isTextEmpty(arg5.c)) {
                    v0_1.put("label", arg5.c);
                }

                v0_1.put("value", Long.valueOf(arg5.d));
                v0_1.put("ext_value", Long.valueOf(arg5.e));
                if (!android.support.design.a.isTextEmpty(arg5.i)) {
                    v0_1.put("ext_json", arg5.i);
                }

                v0_1.put("user_id", Long.valueOf(arg5.f));
                v0_1.put("timestamp", Long.valueOf(arg5.g));
                v0_1.put("session_id", Long.valueOf(arg5.h));
                v0_2 = this.j.insert("event", null, v0_1);
            }
        }
        return v0_2;
    }

    final long a(String arg5, String arg6) {
        long v0_2;
        synchronized (this) {
            ContentValues v0_1 = new ContentValues();
            v0_1.put("log_type", arg5);
            v0_1.put("value", arg6);
            v0_2 = this.j.insert("mon_log", null, v0_1);
        }
        return v0_2;
    }

    public final long a(z arg7) {
        long v0_2;
        int v0 = 0;
        synchronized (this) {
            if (this.j == null || !this.j.isOpen()) {
                Logger.w("AppLog", "db not establish and open");
                v0_2 = -1;
            } else {
                if (arg7.g) {
                    v0 = 1;
                }
                ContentValues v1 = new ContentValues();
                v1.put("value", arg7.b);
                v1.put("timestamp", Long.valueOf(arg7.c));
                v1.put("duration", Integer.valueOf(0));
                v1.put("non_page", Integer.valueOf(v0));
                v1.put("app_version", arg7.d);
                v1.put("version_code", Integer.valueOf(arg7.e));
                v0_2 = this.j.insert("session", null, v1);
            }
        }
        return v0_2;
    }

    public final long a(r arg7, long arg8) {
        long v0_3;
        ContentValues v0_2;
        synchronized (this) {
            if (this.j != null && (this.j.isOpen())) {
                try {
                    v0_2 = new ContentValues();
                    v0_2.put("pausetime", Long.valueOf(arg8));
                    this.j.update("session", v0_2, "_id = ?", new String[]{String.valueOf(arg7.c)});
                } catch (Exception v0_1) {
                    Logger.w("AppLog", "update session pausetime exception: " + v0_1);
                }

                try {
                    v0_2 = new ContentValues();
                    v0_2.put("name", arg7.a);
                    v0_2.put("duration", Integer.valueOf(arg7.b));
                    v0_2.put("session_id", Long.valueOf(arg7.c));
                    v0_3 = this.j.insert("page", null, v0_2);
                    return v0_3;
                } catch (Exception v0_1) {
                    Logger.w("AppLog", "insert page exception: " + v0_1);
                    v0_3 = 0;
                    return v0_3;
                }
            }
            Logger.w("AppLog", "db not establish and open");
            v0_3 = -1;
            return v0_3;
        }
    }

    final long a(long arg6, String arg8, String arg9) {
        long v0_2;
        synchronized (this) {
            ContentValues v0_1 = new ContentValues();
            v0_1.put("log_type", arg8);
            v0_1.put("value", arg9);
            v0_1.put("session_id", Long.valueOf(arg6));
            v0_2 = this.j.insert("misc_log", null, v0_1);
        }
        return v0_2;
    }

    private JSONArray a(long arg26, JSONObject arg28, JSONObject arg29) {
        int v4_2;
        String v4_1;
        String v2_3;
        JSONArray v19;
        long v14;
        Cursor v12_1;
        JSONArray v11;
        long v16;
        String v9;
        String v10;
        String[] v21;
        String v20;
        String[] v6;
        String v5;
        Cursor v3 = null;
        JSONArray v2 = null;
        long v12 = 0;
        try {
            v5 = "_id > ? AND session_id=?";
            v6 = new String[]{"0", String.valueOf(arg26)};
            v20 = "_id<= ? ";
            v21 = new String[]{"0"};
            v10 = "100";
            v9 = "_id ASC";
            v16 = v12;
            v11 = v2;
            v12_1 = v3;
            while (true) {
                v6[0] = String.valueOf(v16);
                v14 = 0;
                v19 = new JSONArray();
                v3 = this.j.query("misc_log", l.f, v5, v6, null, null, v9, v10);
                v3.getCount();
                v12 = v14;
                while (true) {
                    if (!v3.moveToNext()) {
                        if (v16 == 0) {
                            v4_2 = 0;
                            v2 = v19;
                        } else {
                            v4_2 = 1;
                            v2 = v11;
                        }

                        if (v16 < v12) {
                            try {
                                v21[0] = String.valueOf(v12);
                                this.j.delete("misc_log", v20, v21);
                                if (v4_2 != 0 && v19.length() > 0) {
                                    JSONObject v4_3 = new JSONObject();
                                    v4_3.put("magic_tag", "ss_app_log");
                                    if (arg29 != null) {
                                        v4_3.put("time_sync", arg29);
                                    }

                                    v4_3.put("log_data", v19);
                                    if (arg28 != null) {
                                        v4_3.put("header", arg28);
                                    }

                                    v4_3.put("_gen_time", System.currentTimeMillis());
                                    this.a(v4_3.toString(), 0);
                                }
                            } catch (Exception v4) {
                                return v2;
                            }

                            v16 = v12;
                            v11 = v2;
                            v12_1 = v3;
                            break;
                        } else {
                            l.a(v3);
                            return v2;
                        }
                    }
                    v14 = v3.getLong(0);
                    if (v14 <= 0) {
                        continue;
                    }

                    if (v14 > v12) {
                        v12 = v14;
                    }

                    v2_3 = v3.getString(1);
                    v4_1 = v3.getString(2);
                    if (android.support.design.a.isTextEmpty(v4_1)) {
                        continue;
                    }

                    if (android.support.design.a.isTextEmpty(v2_3)) {
                        continue;
                    }

                    JSONObject v7 = new JSONObject(v4_1);
                    v7.put("log_id", v14);
                    if (!android.support.design.a.isTextEmpty(v2_3)) {
                        v7.put("log_type", v2_3);
                    }
                    v19.put(v7);
                    continue;
                }
            }
        } catch (Exception v2_2) {
            return null;
        } finally {
            l.a(v3);
        }
    }

    final long a(String arg5, int arg6) {
        long v0_2;
        synchronized (this) {
            ContentValues v0_1 = new ContentValues();
            v0_1.put("value", arg5);
            v0_1.put("timestamp", Long.valueOf(System.currentTimeMillis()));
            v0_1.put("retry_count", Integer.valueOf(0));
            v0_1.put("retry_time", Long.valueOf(0));
            v0_1.put("log_type", Integer.valueOf(arg6));
            v0_2 = this.j.insert("queue", null, v0_1);
        }
        return v0_2;
    }

    private static void a(Cursor arg1) {
        if (arg1 != null) {
            try {
                if (arg1.isClosed()) {
                    return;
                }

                arg1.close();
            } catch (Exception v0) {
            }
        }
    }

    private static void a(Cursor arg1, SQLiteDatabase arg2) {
        l.a(arg1);
        if (arg2 != null) {
            try {
                if (!arg2.inTransaction()) {
                    return;
                }

                arg2.endTransaction();
            } catch (Exception v0) {
            }
        }
    }

    public final long a(z arg35, z arg36, JSONObject arg37, boolean arg38, long[] arg39, String[] arg40, com.ss.android.common.applog.AppLog.g arg41, boolean arg42, JSONObject arg43) {
        JSONObject v0;
        JSONObject v18_1;
        long v32;
        long v22;
        long v18;
        String v24;
        String v17;
        long v12_1;
        long v10_1;
        String v30;
        String v29;
        long v20_1;
        int v16_1;
        long v14;
        JSONArray v28;
        JSONArray v27;
        long v4_5;
        String[] v16;
        String v15;
        String v20;
        JSONArray v4_4;
        JSONObject v5_2;
        String v9;
        int v5_1;
        int v4_3;
        JSONArray v7;
        Cursor v5;
        Cursor v6 = null;
        String v11;
        JSONArray v26;
        JSONObject v25;
        JSONObject v4_2;
        String[] v8;
        synchronized (this){
            if (this.j != null && (this.j.isOpen())) {
            } else {
                Logger.w("AppLog", "db not establish and open");
                return -1;
            }
            Cursor v13 = null;
            try {
                v8 = new String[]{String.valueOf(arg35.a)};
                if (!android.support.design.a.isTextEmpty(arg35.d) && arg35.e > 0) {
                    v4_2 = new JSONObject(arg37, AppLog.a);
                    v4_2.put("app_version", arg35.d);
                    v4_2.put("version_code", arg35.e);
                    arg37 = v4_2;
                }

                this.j.beginTransaction();
                v25 = new JSONObject();
                v26 = new JSONArray();
                v11 = "_id ASC";
                int v7_1;
                if (!arg35.g && !arg38) {
                    v6 = this.j.query("page", l.b, "session_id = ?", v8, null, null, v11, "500");
                    v7 = new JSONArray();
                    v4_3 = 0;
                    v5_1 = 0;
                    while (v6.moveToNext()) {
                        v9 = v6.getString(1);
                        int v10 = v6.getInt(2);
                        if (android.support.design.a.isTextEmpty(v9)) {
                            continue;
                        }

                        if (v10 <= 0) {
                            continue;
                        }

                        JSONArray v12 = new JSONArray();
                        v12.put(0, v9);
                        v12.put(1, v10);
                        v4_3 += v10;
                        ++v5_1;
                        v7.put(v12);
                    }
                    v6.close();
                    v6 = null;
                    if (v5_1 > 0) {
                        v5_2 = new JSONObject();
                        v5_2.put("duration", v4_3);
                        v5_2.put("datetime", AppLog.b(arg35.c));
                        v5_2.put("session_id", arg35.b);
                        v5_2.put("activites", v7);
                        if (arg41 != null) {
                            arg41.c_();
                        } else {
                        }
                        v4_4 = new JSONArray();
                        v4_4.put(v5_2);
                        v25.put("terminate", v4_4);
                        if (AppLog.o > 0) {
                            v25.put("launch_from", AppLog.o);
                            AppLog.o = 0;
                        }
                        v7_1 = 0;
                        v5 = v6;
                    } else {
                        v7_1 = 1;
                        v5 = v6;
                    }
                } else {
                    v7_1 = 0;
                    v5 = v13;
                }

                    label_137:
                    v20 = "200";
                    v15 = "session_id = ?";
                    if (!arg38) {
                        v16 = v8;
                    } else if (arg39[0] > 0) {
                        v15 = "_id > ? AND session_id=?";
                        v16 = new String[]{String.valueOf(arg39[0]), String.valueOf(arg35.a)};
                    } else {
                        v16 = v8;
                    }
                    v6 = this.j.query("event", l.e, v15, v16, null, null, v11, v20);
                    v4_5 = 0;
                    v27 = new JSONArray();
                    v28 = new JSONArray();
                    v14 = v4_5;
                    v16_1 = 0;
                    v5_1 = 0;
                    v4_3 = 0;
                    while (true) {
                        if (!v6.moveToNext()) {
                            break;
                        }

                        v20_1 = v6.getLong(0);
                        v29 = v6.getString(1);
                        v30 = v6.getString(2);
                        v9 = null;
                        if (!v6.isNull(3)) {
                            v9 = v6.getString(3);
                        }

                        v10_1 = 0;
                        if (!v6.isNull(4)) {
                            v12_1 = v6.getLong(4);
                        } else {
                            v12_1 = v10_1;
                        }
                        v10_1 = 0;
                        if (!v6.isNull(5)) {
                            v10_1 = v6.getLong(5);
                        }

                        v17 = null;
                        if (!v6.isNull(6)) {
                            v24 = v6.getString(6);
                        } else {
                            v24 = v17;
                        }
                        v18 = 0;
                        if (!v6.isNull(7)) {
                            v22 = v6.getLong(7);
                        } else {
                            v22 = v18;
                        }
                        v32 = v6.getLong(8);
                        if (v14 < v20_1) {
                            v14 = v20_1;
                        }

                        v18_1 = null;
                        JSONObject v17_2;
                        if (android.support.design.a.isTextEmpty(v24)) {
                            v17_2 = v18_1;
                        } else {
                            v0 = new JSONObject(v24);
                            v17_2 = v0;
                        }
                        if (v17_2 == null) {
                            v17_2 = new JSONObject();
                        }
                        if (v17_2.optInt("_event_v3", 0) == 1 && !android.support.design.a.isTextEmpty(v29) && (v29.equalsIgnoreCase("event_v3"))) {
                            JSONObject v9_2 = new JSONObject();
                            if (v17_2.has("nt")) {
                                v9_2.put("nt", v17_2.optInt("nt"));
                            }
                            v17_2.remove("nt");
                            v17_2.remove("_event_v3");
                            if (v22 > 0) {
                                v9_2.put("user_id", v22);
                            }
                            v9_2.put("event", v30);
                            v9_2.put("params", v17_2);
                            v9_2.put("event_id", v20_1);
                            v9_2.put("session_id", arg35.b);
                            v9_2.put("datetime", AppLog.b(v32));
                            v28.put(v9_2);
                            ++v4_3;
                        } else {
                            v17_2.put("category", v29);
                            v17_2.put("tag", v30);
                            if (!android.support.design.a.isTextEmpty(v9)) {
                                v17_2.put("label", v9);
                            }

                            if (v12_1 != 0) {
                                v17_2.put("value", v12_1);
                            }

                            if (v10_1 != 0) {
                                v17_2.put("ext_value", v10_1);
                            }

                            if (v22 > 0) {
                                v17_2.put("user_id", v22);
                            }

                            v17_2.put("session_id", arg35.b);
                            v17_2.put("datetime", AppLog.b(v32));
                            v17_2.put("event_id", v20_1);
                            v27.put(v17_2);
                            ++v5_1;
                        }
                        ++v16_1;
                        continue;
                    }

                    try {
                        if (v5_1 > 0) {
                            v25.put("event", v27);
                        }

                        if (v4_3 > 0) {
                            AppLog.r();
                            v25.put("event_v3", v28);
                        }

                        if (v16_1 > 0) {
                            v5_1 = 1;
                        } else {
                            v5_1 = v7_1;
                        }
                        v4_4 = this.a(arg35.a, arg37, arg43);
                        if (v4_4 != null && v4_4.length() > 0) {
                            v25.put("log_data", v4_4);
                            v5_1 = 1;
                        }

                        if (arg41 == null) {

                        } else {
                            arg41.a(arg35.a, arg35.b, v25);
                        }
                        if (arg35.h) {
                            v4_3 = 0;
                        } else {
                            v4_3 = v5_1;
                        }
                        if ((arg38) && (arg42)) {
                            v4_3 = 0;
                        }
                        if (v4_3 != 0 && (v25.isNull("terminate"))) {
                            v4_2 = new JSONObject();
                            v4_2.put("datetime", AppLog.b(arg35.c));
                            v4_2.put("session_id", arg35.b);
                            if (arg35.g) {
                                v4_2.put("is_background", true);
                            }
                            v26.put(v4_2);
                        }

                        if (arg36 != null && !arg36.g) {
                            v4_2 = new JSONObject();
                            v4_2.put("datetime", AppLog.b(arg36.c));
                            v4_2.put("session_id", arg36.b);
                            v26.put(v4_2);
                        }

                        if (v16_1 >= 200) {
                            this.j.delete("event", "session_id= ? AND _id<= ?", new String[]{String.valueOf(arg35.a), String.valueOf(v14)});
                            arg39[0] = v14;
                        } else {
                            this.j.delete("event", "session_id = ?", v8);
                            if (arg42) {
                                this.j.delete("session", "_id = ?", v8);
                            }
                        }

                        if (arg42) {
                            this.j.delete("page", "session_id = ?", v8);
                            this.j.delete("misc_log", "session_id = ?", v8);
                        }

                        if (v26.length() > 0) {
                            v25.put("launch", v26);
                        }

                        v4_5 = 0;
                        if (!v25.isNull("terminate") || !v25.isNull("event") || !v25.isNull("launch") || !v25.isNull("item_impression") || !v25.isNull("log_data") || !v25.isNull("event_v3")) {
                            v25.put("magic_tag", "ss_app_log");
                            if (arg43 != null) {
                                v25.put("time_sync", arg43);
                            }

                            v25.put("header", arg37);
                            v25.put("_gen_time", System.currentTimeMillis());
                            String v4_6 = v25.toString();
                            arg40[0] = v4_6;
                            v4_5 = this.a(v4_6, 0);
                        }
                        this.j.setTransactionSuccessful();
                        l.a(v6, this.j);
                        return v4_5;
                    } catch (Exception v4_1) {
                        Logger.w("AppLog", "batchSession exception " + v4_1);
                    } finally {
                        l.a(v6, this.j);
                        return 0;
                    }
                } catch (Exception v4_1) {
                    Logger.w("AppLog", "onLogSessionBatchEvent exception: " + v4_1);
                    return 0;
                } finally {
                l.a(v6, this.j);
                }
            }
        }

        public final long a (JSONObject arg13, JSONObject arg14){
            long v0_5;
            Cursor v9_1;
            String v7;
            String v6;
            long v4;
            long v2;
            JSONArray v1_2;
            Cursor v0_2 = null;
            long v10 = 0;
            String v9 = null;
            synchronized (this) {
                try {
                    v0_2 = this.j.query("mon_log", l.g, null, null, null, null, "_id ASC", "100");
                    v1_2 = new JSONArray();
                    v2 = v10;
                    while (true) {
                        if (!v0_2.moveToNext()) {
                            v0_2.close();
                            if (v2 > v10) {
                                this.j.delete("mon_log", "_id<= ?", new String[]{String.valueOf(v2)});
                            }
                            if (v1_2.length() > 0) {
                                JSONObject v0_3 = new JSONObject();
                                v0_3.put("magic_tag", "ss_app_log");
                                if (arg14 != null) {
                                    v0_3.put("time_sync", arg14);
                                }

                                v0_3.put("data", v1_2);
                                if (arg13 != null) {
                                    v0_3.put("header", arg13);
                                }
                                v9 = v0_3.toString();
                            }

                            if (v9 != null) {
                                v0_5 = this.a(v9, 2);
                            } else {
                                v0_5 = v10;
                            }
                            return v0_5;
                        }

                        v4 = v0_2.getLong(0);
                        v6 = v0_2.getString(1);
                        v7 = v0_2.getString(2);
                        if (v2 < v4) {
                            v2 = v4;
                        }
                        JSONObject v8 = new JSONObject(v7);
                        v8.put("log_id", v4);
                        if (!android.support.design.a.isTextEmpty(v6)) {
                            v8.put("log_type", v6);
                        }
                        v1_2.put(v8);
                        continue;
                    }
                } catch (Exception v1_1) {
                    return 0;
                } finally {
                    l.a(v0_2);
                }
            }
        }

        public final q a ( long arg14){
            int v2;
            q v0_2;
            Cursor v1 = null;
            q v9 = null;
            synchronized (this) {
                if (this.j != null && (this.j.isOpen())) {
                    try {
                        v1 = this.j.query("queue", l.c, "_id > ?", new String[]{String.valueOf(arg14)}, null, null, "_id ASC", "1");
                        if (v1.moveToNext()) {
                            v0_2 = new q();
                            v0_2.a = ((long) v1.getInt(0));
                            v0_2.b = v1.getString(1);
                            if (v1.getInt(2) > 0) {
                                v2 = 1;
                            } else {
                                v2 = 0;
                            }
                            v1.getLong(3);
                            v1.getInt(4);
                            v1.getLong(5);
                            v0_2.c = v1.getInt(6);
                            if (v0_2.c != 0) {
                            } else if (v2 == 0) {
                            } else {
                                v0_2.c = 1;
                            }
                        } else {
                            v0_2 = v9;
                        }

                        return v0_2;
                    } catch (Exception v0_1) {
                        Logger.w("AppLog", "getLog exception " + v0_1);
                        return null;
                    } finally {
                        l.a(v1);
                    }
                }

                Logger.w("AppLog", "db not establish and open");
                return null;
            }
        }

        public final boolean a ( long arg14, boolean arg16){
            Cursor v1 = null;
            String[] v4;
            boolean v0_1;
            Cursor v10 = null;
            int v9 = 1;
            synchronized (this) {
                if (this.j != null && (this.j.isOpen())) {
                    if (arg14 <= 0) {
                        return false;
                    } else {
                        try {
                            v4 = new String[]{String.valueOf(arg14)};
                            if (!arg16) {
                                int v0_2 = 3;
                                String[] v2 = new String[v0_2];
                                v2[0] = "timestamp";
                                v2[1] = "retry_count";
                                v2[2] = "retry_time";
                                v1 = this.j.query("queue", v2, "_id = ?", v4, null, null, null);
                                if (!v1.moveToNext()) {
                                    l.a(v1);
                                    return false;
                                } else {
                                    try {
                                        long v2_1 = v1.getLong(0);
                                        v0_2 = v1.getInt(1);
                                        long v6 = System.currentTimeMillis();
                                        if (v6 - v2_1 < 432000000 && v0_2 < 5) {
                                            ContentValues v2_2 = new ContentValues();
                                            v2_2.put("retry_count", Integer.valueOf(v0_2 + 1));
                                            v2_2.put("retry_time", Long.valueOf(v6));
                                            this.j.update("queue", v2_2, "_id = ?", v4);
                                            l.a(v1);
                                            return true;
                                        }
                                        l.a(v1);

                                    } catch (Exception v0_3) {
                                        Logger.w("AppLog", "onLogSent excepiton: " + v0_3);
                                        l.a(v1);
                                        v9 = 0;
                                    }
                                }
                            }
                            if (v9 != 0) {
                                try {
                                    this.j.delete("queue", "_id = ?", v4);
                                } catch (Throwable v0) {
                                }
                                Logger.d("AppLog", "delete app_log: " + arg14);
                                return false;
                            }
                            return false;
                        } catch (Throwable v0) {
                            return false;
                        } finally {
                            l.a(v1);
                        }
                    }
                }
                Logger.w("AppLog", "db not establish and open");
                return false;
            }


        }

        public final z b ( long arg14){
            boolean v2;
            z v0_2;
            Cursor v1 = null;
            String[] v4;
            String v3;
            String v8;
            String v7;
            z v9 = null;
            synchronized (this) {
                if (this.j != null && (this.j.isOpen())) {
                    try {
                        label_15:
                        v7 = "_id DESC";
                        v8 = "1";
                        if (arg14 > 0) {
                            v3 = "_id < ?";
                            v4 = new String[]{String.valueOf(arg14)};
                        } else {
                            v4 = ((String[]) null);
                            v3 = ((String) null);
                        }
                        v1 = this.j.query("session", l.d, v3, v4, null, null, v7, v8);
                        if (v1.moveToNext()) {
                            v0_2 = new z();
                            v0_2.a = ((long) v1.getInt(0));
                            v0_2.b = v1.getString(1);
                            v0_2.c = v1.getLong(2);
                            if (v1.getInt(4) > 0) {
                                v2 = true;
                            } else {
                                v2 = false;
                            }
                            v0_2.g = v2;
                            v0_2.d = v1.getString(5);
                            v0_2.e = v1.getInt(6);
                            v0_2.f = ((long) v1.getInt(7));
                            if (v1.getInt(8) > 0) {
                                v2 = true;
                            } else {
                                v2 = false;
                            }
                            v0_2.h = v2;
                            v0_2.i = false;
                        } else {
                            v0_2 = v9;
                        }
                        return v0_2;

                    } catch (Exception v0_1) {
                        Logger.w("AppLog", "getLastSession exception " + v0_1);
                        return null;
                    } finally {
                        l.a(v1);
                    }
                }
                Logger.w("AppLog", "db not establish and open");
                return null;
            }
        }

        public final void b () {
            synchronized (this) {
                if (this.j != null && (this.j.isOpen())) {
                    String v0_1 = "timestamp <= ? OR retry_count > 5";
                    String[] v1 = new String[]{String.valueOf(System.currentTimeMillis() - 432000000)};
                    try {
                        this.j.delete("queue", v0_1, v1);
                    } catch (Exception v0_2) {
                        Logger.d("AppLog", "delete expire log error:" + v0_2);
                    }
                } else {
                    Logger.w("AppLog", "db not establish and open");
                }
            }
        }

        private void c () {
            synchronized (this) {
                try {
                    if (this.j == null) {
                        return;
                    }

                    if (!this.j.isOpen()) {
                        return;
                    }
                    this.j.close();
                    this.j = null;
                } catch (Throwable v0) {
                    Logger.w("AppLog", "closeDatabase error: " + v0);
                }
            }
        }

        public final void c ( long arg6){
            synchronized (this) {
                if (this.j != null && (this.j.isOpen())) {
                    try {
                        String[] v1 = new String[]{String.valueOf(arg6)};
                        ContentValues v2 = new ContentValues();
                        v2.put("launch_sent", Integer.valueOf(1));
                        this.j.update("session", v2, "_id=?", v1);
                    } catch (Exception v0_1) {
                        Logger.w("AppLog", "setSessionLaunchSent exception: " + v0_1);
                    }
                } else {
                    Logger.w("AppLog", "db not establish and open");
                }
            }
        }
    }

